//package com.techzhi.addres_score.service.impl;
//
//
//
//import com.techzhi.addres_score.mapper.CityMapper;
//import com.techzhi.addres_score.mapper.DistrictMapper;
//import com.techzhi.addres_score.mapper.ProvinceMapper;
//import com.techzhi.addres_score.model.City;
//import com.techzhi.addres_score.model.District;
//import com.techzhi.addres_score.model.Province;
//import com.techzhi.addres_score.service.AddressCacheService;
//import lombok.AllArgsConstructor;
//import lombok.NoArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//@Service
//@Slf4j
//@NoArgsConstructor
//@AllArgsConstructor
//public class AddressCacheServiceImpl implements AddressCacheService {
//
//    @Autowired
//    private ProvinceMapper provinceMapper;
//    @Autowired
//    private CityMapper cityMapper;
//    @Autowired
//    private DistrictMapper districtMapper;
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
//
//    @Override
//    public void refreshAllCache() {
//        refreshProvinceCache();
//        refreshCityCache();
//        refreshDistrictCache();
//        log.info("所有地址缓存刷新完成");
//    }
//
//    @Override
//    public void refreshProvinceCache() {
//        List<Province> provinces = provinceMapper.selectAllOrderByCode();
//        // 缓存所有省份列表
//        redisTemplate.opsForValue().set("provinces::all", provinces, 24, TimeUnit.HOURS);
//        // 缓存每个省份的详细信息
//        for (Province province : provinces) {
//            redisTemplate.opsForValue().set("provinces::" + province.getCode(), province, 24, TimeUnit.HOURS);
//        }
//        log.info("省份缓存刷新完成，共{}条数据", provinces.size());
//    }
//
//    @Override
//    public void refreshCityCacheByProvince(String provinceCode) {
//        List<City> cities = cityMapper.selectByProvinceCode(provinceCode);
//        // 缓存该省份下的城市列表
//        redisTemplate.opsForValue().set("cities::" + provinceCode, cities, 24, TimeUnit.HOURS);
//        // 缓存每个城市的详细信息
//        for (City city : cities) {
//            redisTemplate.opsForValue().set("city::" + city.getCode(), city, 24, TimeUnit.HOURS);
//        }
//        log.info("省份[{}]的城市缓存刷新完成，共{}条数据", provinceCode, cities.size());
//    }
//
//    private void refreshCityCache() {
//        List<Province> provinces = provinceMapper.selectAllOrderByCode();
//        for (Province province : provinces) {
//            refreshCityCacheByProvince(province.getCode());
//        }
//    }
//
//    private void refreshDistrictCache() {
//        List<City> cities = cityMapper.selectList(null);
//        for (City city : cities) {
//            refreshDistrictCacheByCity(city.getCode());
//        }
//    }
//
//    @Override
//    public void refreshDistrictCacheByCity(String cityCode) {
//        List<District> districts = districtMapper.selectByCityCode(cityCode);
//        // 缓存该城市下的区县列表
//        redisTemplate.opsForValue().set("districts::" + cityCode, districts, 24, TimeUnit.HOURS);
//        // 缓存每个区县的详细信息
//        for (District district : districts) {
//            redisTemplate.opsForValue().set("district::" + district.getCode(), district, 24, TimeUnit.HOURS);
//        }
//        log.info("城市[{}]的区县缓存刷新完成，共{}条数据", cityCode, districts.size());
//    }
//
//    @Override
//    public void clearAllCache() {
//        redisTemplate.delete(redisTemplate.keys("provinces::*"));
//        redisTemplate.delete(redisTemplate.keys("cities::*"));
//        redisTemplate.delete(redisTemplate.keys("city::*"));
//        redisTemplate.delete(redisTemplate.keys("districts::*"));
//        redisTemplate.delete(redisTemplate.keys("district::*"));
//        log.info("所有地址缓存已清除");
//    }
//}
